package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanProjectDto;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanProjectService;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingPlanProjectService nursingPlanProjectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.insert(nursingPlan);
        List<NursingPlanProjectDto> list = nursingPlanDto.getProjectPlans();
        Long planId = nursingPlan.getId();
        nursingPlanProjectService.saveBatch(list,planId);
    }

    @Override
    public List<NursingPlanVo> findAll() {
        List<NursingPlanVo> list = nursingPlanMapper.selectAll();
        return list;
    }

    @Override
    public PageResponse<NursingPlanVo> findByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.selectByPage(name,status);
        PageResponse<NursingPlanVo> response = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return response;
    }

    @Override
    public NursingPlanVo findById(Integer id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(id);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        Long planId = nursingPlan.getId();
        List<NursingPlanProjectVo> list = nursingPlanProjectService.findByPlanId(planId);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    @Override
    public void delete(Integer id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(id);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanVo, NursingPlan.class);
        Long planId = nursingPlan.getId();
        nursingPlanProjectService.delete(planId);
        nursingPlanMapper.delete(id);
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto, Integer id) {
        nursingPlanMapper.update(nursingPlanDto);
        Long planId = Long.valueOf(id);
        nursingPlanProjectService.delete(planId);
        List<NursingPlanProjectDto> list = nursingPlanDto.getProjectPlans();
        nursingPlanProjectService.saveBatch(list,planId);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        Long planId = Long.valueOf(id);
        nursingPlan.setId(planId);
        nursingPlan.setStatus(status);
        NursingPlanDto nursingPlanDto = BeanUtil.toBean(nursingPlan, NursingPlanDto.class);
        nursingPlanMapper.update(nursingPlanDto);
    }

}
